﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Linq.Extensions;
using Abp.UI;
using AutoMapper;
using MyCompanyName.AbpZeroTemplate.Authorization;
using MyCompanyName.AbpZeroTemplate.BrandApp.Dto;
using MyCompanyName.AbpZeroTemplate.CategoryApp.Dto;
using MyCompanyName.AbpZeroTemplate.Entities;
using MyCompanyName.AbpZeroTemplate.IRepositories;

namespace MyCompanyName.AbpZeroTemplate.BrandApp
{
    [AbpAuthorize(AppPermissions.Pages_Brand)]
    public class BrandAppService : AbpZeroTemplateAppServiceBase, IBrandAppService
    {
        private IBrandRepository _brandRepository;
        public BrandAppService(IBrandRepository brandRepository)
        {
            _brandRepository = brandRepository;
        }

        [AbpAuthorize(AppPermissions.Pages_Brand_Create)]
        public void CreateBrand(CreateBrandInput input)
        {
            int count = _brandRepository.Count(a => a.Name.Equals(input.Name));
            if (count > 0)
            {
                throw new UserFriendlyException("品牌名称已存在！");
            }
            _brandRepository.Insert(new Brand()
            {
                Name = input.Name,
                Description = input.Description
            });
        }

        [AbpAuthorize(AppPermissions.Pages_Brand_Delete)]
        public void DeleteBrand(EntityRequestInput<Guid> input)
        {
            var category = _brandRepository.Get(input.Id);
            _brandRepository.Delete(category);
        }

        public BrandOutput GetBrandForEdit(EntityRequestInput<Guid> input)
        {
            var brand = _brandRepository.Get(input.Id);
            return new BrandOutput()
            {
                Id = brand.Id,
                Name = brand.Name,
                Description = brand.Description
            };
        }

        public PagedResultOutput<BrandOutput> GetBrands(GetBrandsInput input)
        {
            //创建映射
            Mapper.CreateMap<Brand, BrandOutput>();
            var result = _brandRepository.GetAll();
            if (!string.IsNullOrWhiteSpace(input.Filter))
            {
                result = result.Where(a => a.Name.Contains(input.Filter));
            }

            int totalCount = result.Count();
            var list = result.OrderBy(input.Sorting).PageBy(input).ToList();//分页
            return new PagedResultOutput<BrandOutput>(
                totalCount,
                Mapper.Map<List<BrandOutput>>(list)
                );
        }

        [AbpAuthorize(AppPermissions.Pages_Brand_Edit)]
        public void UpdateBrand(EditBrandInput input)
        {
            int count = _brandRepository.Count(a => a.Name.Equals(input.Name) && a.Id != input.Id);
            if (count > 0)
            {
                throw new UserFriendlyException("品牌名称已存在！");
            }
            var brand = _brandRepository.Get(input.Id);
            brand.Name = input.Name;
            brand.Description = input.Description;
        }
    }
}
